﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;

namespace Hangjing.WebCommon
{
    /// <summary>
    /// 订单服务实现
    /// </summary>
    public class OrderServiceImpl
    {
        /// <summary>
        /// 手动完成订单
        /// </summary>
        /// <param name="orderid"></param>
        /// <param name="did"></param>
        /// <returns></returns>
        public int manComplete(string orderid, int did)
        {
            new Custorder().manComplete(orderid, did);
            shareReward(orderid);
            return 1;
        }

        /// <summary>
        /// 配送完成相关操作
        /// </summary>
        /// <param name="orderid"></param>
        /// <param name="did"></param>
        /// <returns></returns>
        public int deliverComplete(string orderid, int did)
        {
            new Custorder().deliverCompleteX(orderid, did);
            shareReward(orderid);
            return 1;
        }

        /// <summary>
        /// 分享奖励
        /// </summary>
        public void shareReward(string orderid) {
            CustorderInfo model = new Custorder().GetModel(orderid);
            if (model.OrderStatus == 3)
            {
                ECustomerInfo user = new ECustomer().GetModel(model.UserId);
                if (new Custorder().GetCount("userid="+model.UserId+ " and OrderStatus=3") > 1 )
                {
                    //只有第一个单才有奖励
                    return;
                }

                if (user != null && user.RID.Length > 0 && user.RID != "0")
                {
                    Hangjing.AppLog.AppLog.Info("" + user.DataID + "完成订单，有推荐有：" + user.RID);
                    ECustomerInfo recommender = new ECustomer().GetModel(Convert.ToInt32(user.RID));
                    if (recommender != null)
                    {
                        //todo 获取配置信息
                        promotion_share_config promotion_Share_Config = new PromotionShareConfigDal().GetEntity(1);
                        if (promotion_Share_Config != null && promotion_Share_Config.stauts ==1)
                        {
                            UserShareIncomeDal userShareIncomeDal = new UserShareIncomeDal();
                            //1,上级获取奖励。
                            {
                                user_share_income_log log = new user_share_income_log();
                                log.c_t = DateTime.Now;
                                log.u_t = DateTime.Now;
                                log.source = 1;
                                log.msg = "你推荐的会员:"+user.DataID+"下单成功，获取奖励";
                                log.user_limit = 0;
                                log.user_id = recommender.DataID;
                                log.from_user_id = user.DataID;
                                log.change_limit = promotion_Share_Config.first_level_price;

                                userShareIncomeDal.changeLimit(log, LimitOperation.add);
                            }
                            //2, 上上 级获取奖励
                            {
                                if (recommender.RID.Length > 0 && recommender.RID != "0") {

                                    ECustomerInfo superRecommender = new ECustomer().GetModel(Convert.ToInt32(recommender.RID));
                                    if (superRecommender != null)
                                    {
                                        user_share_income_log log = new user_share_income_log();
                                        log.c_t = DateTime.Now;
                                        log.u_t = DateTime.Now;
                                        log.source = 1;
                                        log.msg = "你推荐的会员:" + recommender.DataID + "的下级"+user.DataID+"下单成功，获取奖励";
                                        log.user_limit = 0;
                                        log.user_id = superRecommender.DataID;
                                        log.from_user_id = recommender.DataID;
                                        log.change_limit = promotion_Share_Config.second_level_price;

                                        userShareIncomeDal.changeLimit(log, LimitOperation.add);
                                    }
                                }
                            }





                        }
                    }
                }
            }

        }
    }
}
